#define _CRT_SECURE_NO_WARNINGS 1
#include "Snake.h"

//定位光标
void SetPos(int x, int y)
{
	//获取屏幕句柄
	HANDLE houtput = GetStdHandle(STD_OUTPUT_HANDLE);

	//定位新坐标
	COORD pos = { x,y };

	//设置光标位置
	SetConsoleCursorPosition(houtput, pos);
}

//初始化窗口
void InitWindow()
{
	//设置窗口大小
	system("mode con cols=100 lines=30");

	//设置窗口名称
	system("title 贪吃蛇");

	//隐藏光标
	//获取屏幕的句柄（操作权）
	HANDLE houtput = GetStdHandle(STD_OUTPUT_HANDLE);

	//定义一个光标信息的结构体
	CONSOLE_CURSOR_INFO cursor_info = { 0 };

	//获取屏幕光标的操作权
	GetConsoleCursorInfo(houtput, &cursor_info);

	//修改光标的可见性
	cursor_info.bVisible = false;

	//设置光标信息
	SetConsoleCursorInfo(houtput, &cursor_info);
}

//欢迎界面的打印
void welcome()
{
	//定位光标
	SetPos(40, 15);
	printf("欢迎来到贪吃蛇\n");
	SetPos(35, 25);
	system("pause");
	system("cls");
	
	//打印帮助信息
	SetPos(25, 15);
	wprintf(L"用 ↑. ↓ . ← . → 来控制蛇的移动，按Shift加速，Ctrl减速\n");
	SetPos(35, 25);
	system("pause");
	system("cls");
}

//地图的打印
void CreatMap()
{
	int i = 0;
	//上墙
	for (i = 0; i < 29; i++)
	{
		wprintf(L"%lc ", WALL);
	}
	//下墙
	SetPos(0, 26);
	for (i = 0; i < 29; i++)
	{
		wprintf(L"%lc ", WALL);
	}
	//左墙
	for (i = 1; i < 27; i++)
	{
		SetPos(0, i);
		wprintf(L"%lc", WALL);
	}
	//右墙
	for (i = 1; i < 27; i++)
	{
		SetPos(56,i);
		wprintf(L"%lc", WALL);
	}
}

//蛇身的初始化
void InitSnake(pSnake ps)
{
	//假设初始蛇有两个节点
	int i = 0;
	pSnakeNode newnode = NULL;
	for (i = 0; i < 2; i++)
	{
		//创建蛇的节点
		newnode = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (newnode == NULL)
		{
			perror("InitSnake()::malloc()");
			return;
		}
		newnode->Next = NULL;

		//设置坐标
		//默认蛇节点从(12,4)开始
		newnode->x = POX_X + 2 * i;
		newnode->y = POX_Y;

		//利用头插法将新节点连接到链表中
		if (ps->_pSnake == NULL)
		{
			//空链表
			ps->_pSnake = newnode;
		}
		else
		{
			//非空链表
			newnode->Next = ps->_pSnake;
			ps->_pSnake = newnode;
		}
	}

	//打印蛇身体
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		//定位坐标
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->Next;
	}

	//设置蛇的属性
	ps->_dir = RIGHT;//默认方向向右
	ps->_Statue = OK;//默认状态OK
	ps->_Food_Weight = 10;//默认一个食物10分
	ps->_score = 0;//默认起始总分数为0分
	ps->_Sleep_Time = 200;//默认休眠时间，单位毫秒
}

//食物的初始化
void InitFood(pSnake ps)
{
	int x = 0;
	int y = 0;
again:
	do
	{
		//食物的坐标要随机生成
		x = rand() % 53 + 2;//产生2~54的随机数
		y = rand() % 25 + 1;//产生1~26的随机数
	} while (x % 2 != 0);

	//食物不能和蛇的身体重叠
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		if (cur->x == x && cur->y == y)
		{
			goto again;
		}
		cur = cur->Next;
	}

	//创建食物节点
	pSnakeNode food = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (food == NULL)
	{
		perror("InitFood()::malloc()");
		return;
	}
	food->Next = NULL;
	food->x = x;
	food->y = y;

	//打印食物
	SetPos(food->x, food->y);
	wprintf(L"%lc", FOOD);

	//将食物让pSnake贪吃蛇维护
	ps->_pFood = food;
}

//游戏的初始化
void GameInit(pSnake ps)
{
	//初始化窗口
	InitWindow();

	//欢迎界面的打印
	welcome();

	//地图的打印
	CreatMap();

	//蛇身的初始化
	InitSnake(ps);

	//食物的初始化
	InitFood(ps);
}

void PrintHelpInfo(pSnake ps)
{
	//定位光标
	SetPos(65, 18);
	wprintf(L"用 ↑. ↓ . ← . → 来控制蛇的移动");
	SetPos(65, 19);
	printf("按Shift加速，Ctrl减速");
	SetPos(65, 20);
	printf("加速可以得到更高的分数");
	SetPos(65, 21);
	printf("小心不要撞到自己和墙");
	SetPos(65, 22);
	printf("ESC退出游戏，SPACE暂定游戏");
}

//暂停
void Suspend_time_out(pSnake ps)
{
	do
	{
		Sleep(300);
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			ps->_Statue = END_NOEMAL;
		}
	} while (1);
}

//下一个位置是不是食物
int NextPositionIsFood(pSnake ps,pSnakeNode pnode)
{
	return ((ps->_pFood->x == pnode->x) && (ps->_pFood->y == pnode->y));
}

//吃食物
void EatFood(pSnake ps,pSnakeNode pnode)
{
	//此时pnode和食物节点是同一个节点，我们可任选一个头插到链表中，然后经另一个给释放掉
	pnode->Next = ps->_pSnake;
	ps->_pSnake = pnode;

	//释放食物节点
	free(ps->_pFood);
	ps->_pFood = NULL;

	//创建一个新食物
	InitFood(ps);

	//吃了一个食物总分数要增加
	ps->_score += ps->_Food_Weight;

	//打印蛇身
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		//定位光标
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->Next;
	}
}

//不是食物
void NoFood(pSnake ps, pSnakeNode pnode)
{
	//将pnode节点作为新的蛇头，然后释放掉贪吃蛇的最后一个节点，并在其位置上打印两个空白字符，覆盖掉原先的蛇身
	pnode->Next = ps->_pSnake;
	ps->_pSnake = pnode;

	pSnakeNode cur = ps->_pSnake;
	while (cur->Next->Next)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->Next;
	}
	//此时cur为倒数第二个节点
	SetPos(cur->Next->x, cur->Next->y);

	//打印两个空格，覆盖原来的蛇身
	printf("  ");
	free(cur->Next);
	cur->Next = NULL;
}

//撞墙
void KillByWall(pSnake ps)
{
	pSnakeNode cur = ps->_pSnake;
	if (cur->x == 0 || cur->x == 56)
	{
		ps->_Statue = KILL_BY_WALL;
		return;
	}
	else if (cur->y == 0 || cur->y == 26)
	{
		ps->_Statue = KILL_BY_WALL;
		return;
	}
	else
	{
		return;
	}
}

//撞到自己
void KillBySelf(pSnake ps)
{
	pSnakeNode next = ps->_pSnake->Next;
	while (next)
	{
		if (ps->_pSnake->x == next->x && ps->_pSnake->y == next->y)
		{
			ps->_Statue = KILL_BY_SELF;
			return;
		}
		next = next->Next;
	}
}

//蛇走一步
void SnakeMove(pSnake ps)
{
	//先创建一个新节点
	pSnakeNode node = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (node == NULL)
	{
		perror("SnskeMove()::malloc()");
		return;
	}

	//计算下一个位置的坐标
	switch (ps->_dir)
	{
	case UP:
	{
		node->x = ps->_pSnake->x;
		node->y = ps->_pSnake->y - 1;
		break;
	}
	case DOWN:
	{
		node->x = ps->_pSnake->x;
		node->y = ps->_pSnake->y + 1;
		break;
	}
	case LEFT:
	{
		node->x = ps->_pSnake->x - 2;
		node->y = ps->_pSnake->y;
		break;
	}
	case RIGHT:
	{
		node->x = ps->_pSnake->x + 2;
		node->y = ps->_pSnake->y;
		break;
	}
	}

	//判断下一个位置是不是食物
	//是食物
	if (NextPositionIsFood(ps,node))
	{
		//吃食物
		EatFood(ps,node);
	}
	//不是食物
	else
	{
		NoFood(ps,node);
	}

	//蛇在走一步的过程中可能会撞到墙或者自己
	//撞墙
	KillByWall(ps);

	//撞到自己
	KillBySelf(ps);
}

//游戏运行
void GameRun(pSnake ps)
{
	//首先在游戏右边打印帮助信息
	PrintHelpInfo(ps);

	//检测按过哪个键
	do
	{
		//打印总分数以及单个食物的分数
		SetPos(69, 7);
		printf("Score：%2d", ps->_score);
		SetPos(69, 8);
		printf("Food Weight：%2d", ps->_Food_Weight);

		if (KEY_PRESS(VK_UP) && ps->_dir != DOWN)
		{
			//向上走
			ps->_dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->_dir != UP)
		{
			//向下走
			ps->_dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_dir != RIGHT)
		{
			//向左走
			ps->_dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_dir != LEFT)
		{
			//向右走
			ps->_dir = RIGHT;
		}
		else if (KEY_PRESS(VK_SHIFT))
		{
			//加速
			//限定速度最大值
			if(ps->_Sleep_Time > 100)
			{
				ps->_Sleep_Time -= 20;
				ps->_Food_Weight += 2;
			}
		}
		else if (KEY_PRESS(VK_CONTROL))
		{
			//减速
			//限定最小分数，分数不能为0
			if(ps->_Food_Weight >= 2)
			{
				ps->_Sleep_Time += 20;
				ps->_Food_Weight -= 2;
			}
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			//暂停
			Suspend_time_out(ps);
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			//退出
			ps->_Statue = END_NOEMAL;
			break;
		}

		//蛇走一步
		SnakeMove(ps);

		Sleep(ps->_Sleep_Time);

	} while (ps->_Statue == OK);
}

//销毁链表
void ReleaseSnake(pSnake ps)
{
	//销毁链表
	pSnakeNode cur = ps->_pSnake;
	pSnakeNode next = NULL;
	while (cur)
	{
		next = cur->Next;
		free(cur);
		cur = next;
	}
	ps = NULL;
}

//游戏结束
//善后工作
void GameOver(pSnake ps)
{
	//判断游戏是怎样结束的
	if (ps->_Statue == END_NOEMAL)
	{
		SetPos(16, 7);
		printf("You voluntarily quit the game");
	}
	else if (ps->_Statue == KILL_BY_WALL)
	{
		SetPos(16, 7);
		printf("You've hit a wall");
	}
	else if (ps->_Statue == KILL_BY_SELF)
	{
		SetPos(16, 7);
		printf("You bumped into yourself");
	}
	//释放节点
	ReleaseSnake(ps);
	ps = NULL;
}